define(['knockout',
    'platform.common',
    'knockout-bsDatetimepicker',
    'knockout-component',
    'anychart-stock', 'anychart-theme-lightblue',
    'bootbox'], function (ko, platform) {

    var bsDTP = require('knockout-bsDatetimepicker');
    var tgOpts = platform.servicebreak("~/rest/tg/getTgDict").responseJSON.responseObject;

    var bootbox = require('bootbox');
    var table = null, chart = null, count = null;

    var VM = function () {
        self = this;

        self.tgOpts = ko.computed(function () {
            return tgOpts;
        });

        self.startDate = ko.observable();
        self.endDate = ko.observable();
        self.fixedTgId = ko.observable();

        self.startTime = new bsDTP.bootstrapDateTimePicker({initialDate: new Date()});
        self.endTime = new bsDTP.bootstrapDateTimePicker({});
        self.init = function () {
            table = null;
            chart = null;
            count = null;
        };
        self.renderSearchArea = function () {//渲染查询区域,未生效
            $('#searchForm').resetForm();
        };
        self.fnReset = function () {//重置
            $('#searchForm').resetForm();
        };
        /**
         * 查询方法
         */
        self.fnSearch = function () {
            if (undefined == this.fixedTgId()) {
                bootbox.alert({message: "请选择指定的台区!", size: 'small'});
                return;
            }
            if (undefined == this.startDate() || undefined == this.endDate()) {
                bootbox.alert({message: "未指定必要的查询时间区间!", size: 'small'});
                return;
            }

            var param = {};
            param.tg_id = this.fixedTgId();
            param.start_date = this.startDate();
            param.end_date = this.endDate();

            platform.servicebreakAsync('~/rest/record/getTranPowerCurveByParam', param).done(function (response) {
                anychart.onDocumentReady(function () {
                    // apply theme
                    // anychart.theme(anychart.themes.lightBlue);
                    anychart.format.inputLocale('zh-cn');//设置国际化
                    anychart.format.outputLocale('zh-cn');
                    anychart.format.inputDateTimeFormat('yyyy-MM-dd');
                    anychart.format.outputDateTimeFormat('yyyy-MM-dd');

                    if (undefined == table || null == table) {
                        table = anychart.data.table('data_date');
                        table.addData(response.responseObject.result);
                        count = response.responseObject.count;
                        // mapping the data
                        mapping_balance = table.mapAs({x: 'data_date', value: 'data_value'});
                        mapping_unblance_k1 = table.mapAs({x: 'data_date', value: 'data_k1_value'});
                        mapping_unblance_k2 = table.mapAs({x: 'data_date', value: 'data_k2_value'});
                        mapping_unblance_k3 = table.mapAs({x: 'data_date', value: 'data_k3_value'});
                        // defining the chart type
                        chart = anychart.stock();
                        // set the series type
                        var series_balance = chart.plot(0).line(mapping_balance);
                        series_balance.name('三相平衡时线损率');
                        var series_unbalance_k1 = chart.plot(0).line(mapping_unblance_k1);
                        series_unbalance_k1.name('一轻两重时线损率');
                        var series_unbalance_k2 = chart.plot(0).line(mapping_unblance_k2);
                        series_unbalance_k2.name('一轻一重一平均时线损率');
                        var series_unbalance_k3 = chart.plot(0).line(mapping_unblance_k3);
                        series_unbalance_k3.name('两重一轻时线损率');

                        // setting the chart title
                        chart.title('台区标杆值曲线');
                        chart.yAxis(0).title('线损率(%)');
                        chart.animation(true);
                        // display the chart
                        chart.container('map');
                        chart.draw();
                    } else {//清空当前数据集，重新load
                        table.removeFirst(count);
                        table.addData(response.responseObject.result);
                        count = response.responseObject.count;
                        chart.container('map');
                        chart.draw();
                    }
                });
            })
        };
    };
    return new VM();
});